// 首字母大写
export const firstLetterCase = (word) => {
	return word.charAt(0).toUpperCase() + word.slice(1)
}

export const formatDate = (
	dateTemplate = 'YYYY-MM-DD hh:mm:ss',
	date = new Date()
) => {
	date = new Date(date)
	if (!(date instanceof Date)) {
		throw new Error('您传入的类型不是时间格式！')
	} else {
		const YYYY = date.getFullYear()
		const M = String(date.getMonth() + 1)
		const MM = M.padStart(2, '0')
		const D = String(date.getDate())
		const DD = D.padStart(2, '0')
		const h = String(date.getHours())
		const hh = h.padStart(2, '0')
		const m = String(date.getMinutes())
		const mm = m.padStart(2, '0')
		const s = String(date.getSeconds())
		const ss = s.padStart(2, '0')
		const str = dateTemplate
			.replace(/Y{4}/g, YYYY)
			.replace(/MM/g, MM)
			.replace(/DD/g, DD)
			.replace(/hh/g, hh)
			.replace(/mm/g, mm)
			.replace(/ss/g, ss)
			.replace(/M/g, M)
			.replace(/D/g, D)
			.replace(/h/g, h)
			.replace(/m/g, m)
			.replace(/s/g, s)
		return str
	}
}

export const sleep = (time) => {
	return new Promise((resolve) => setTimeout(resolve, time))
}

export const getNewId = () => {
	return Date.now().toString() + Math.random().toString().substr(2, 5)
}